package com.ygqh.baby.controller.admin;

import com.foxinmy.weixin4j.exception.WeixinException;
import com.ygqh.baby.ao.DataStatus;
import com.ygqh.baby.ao.Message;
import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.model.BiChannelModel;
import com.ygqh.baby.model.YgCompanyBalanceDetailModel;
import com.ygqh.baby.model.YgUserStatisticsModel;
import com.ygqh.baby.po.YgChannel;
import com.ygqh.baby.service.WxMpAuthorityService;
import com.ygqh.baby.service.YgChannelService;
import com.ygqh.baby.service.YgUserBalanceService;
import com.ygqh.baby.service.YgUserService;
import com.ygqh.baby.utils.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin/source")
public class SourceController extends BaseController<Object, java.lang.Long> {

	@Autowired
	private YgChannelService ygChannelService;
	@Autowired
	private WxMpAuthorityService wxMpAuthorityService;
	@Autowired
	private YgUserService ygUserService;
	@Autowired
	private YgUserBalanceService ygUserBalanceService;


	@RequestMapping("searchSource")
	@ResponseBody
	public Map<String, Object> searchSource(String q, QueryInfo queryInfo, Long channelCategoryId, String sourceCode) throws ParseException {
		ResultSet<YgChannel> result = ygChannelService.search(queryInfo, channelCategoryId, sourceCode, q);
		for (YgChannel channel : result.getResult()) {
			channel.setOrderAmount(0l);
			channel.setOrderPrice(new BigDecimal("0"));
			BiChannelModel channelBi = ygUserBalanceService.findChannelBi(channel.getSourceCode());
			if (channelBi != null) {
				channel.setOrderAmount(channelBi.getOrderAmount());
				channel.setOrderPrice(channelBi.getOrderPrice());

			}
		}
		return result.toDatatables(null);
	}

	@RequestMapping("saveSource")
	@ResponseBody
	public Message saveSource(String source) {
		YgChannel channel = JsonUtils.toObject(source, YgChannel.class);
		channel.setCommissionRate(channel.getCommissionRate().multiply(new BigDecimal(0.01)));
		channel.setForeverRate(channel.getForeverRate().multiply(new BigDecimal(0.01)));
		channel.setCreateBy(getSession().getTrueName());
		channel.setCreateTime(new Date());
		channel.getSourceCode();
	
		ygChannelService.saveOrUpdate(channel);

		return SUCCESS_MESSAGE;
	}

	@RequestMapping("getSource")
	@ResponseBody
	public YgChannel getSource(Long id) {
		return ygChannelService.findById(id);
	}

	@RequestMapping("delSource")
	@ResponseBody
	public Message delSource(Long id) {
		YgChannel channel = new YgChannel();
		channel.setId(id);
		channel.setUpdateBy(getSession().getTrueName());
		channel.setStatus(DataStatus.Delete);
		channel.setUpdateTime(new Date());
		ygChannelService.updateStatus(channel);
		return SUCCESS_MESSAGE;
	}

	@RequestMapping("isExistsSource")
	@ResponseBody
	public Boolean isExistsSource(String sourceCode, Long id) {
		return ygChannelService.isExist(id, sourceCode);

	}

	@RequestMapping("getQrcode")
	@ResponseBody
	public String getQrcode(String sourceCode) throws WeixinException {
		return wxMpAuthorityService.findQrCodeUrl(sourceCode);
	}

	@RequestMapping("channelReport")
	@ResponseBody
	public Map<String, Object> channelReport(String sourceCode, Date startDate, Date endDate) {
		List<Map<String, Object>> list = ygUserService.findUserStatisticsLM(startDate, endDate, sourceCode);
		ResultSet<Map<String, Object>> resultSet = new ResultSet<Map<String, Object>>(1, list);

		return resultSet.toDatatables(null);
	}

	@RequestMapping("channelReportDetail")
	@ResponseBody
	public Map<String, Object> channelReportDetail(String sourceCode, Date startDate, Date endDate) {
		List<YgUserStatisticsModel> list = ygUserService.findUserGrouBySourceCode(sourceCode);
		ResultSet<YgUserStatisticsModel> resultSet = new ResultSet<YgUserStatisticsModel>(1, list);

		return resultSet.toDatatables(null);
	}

	@RequestMapping("findCompanyPreIncomeList")
	@ResponseBody
	public Map<String, Object> findCompanyPreIncomeList(QueryInfo queryInfo, Long userId) {

		ResultSet<YgCompanyBalanceDetailModel> resultList = ygUserBalanceService.findCompanyPreIncomeList(queryInfo, userId);
		return resultList.toDatatables(null);
	}

	@RequestMapping("findCompanyBalanceList")
	@ResponseBody
	public Map<String, Object> findCompanyBalanceList(QueryInfo queryInfo, Long userId) {

		ResultSet<YgCompanyBalanceDetailModel> resultList = ygUserBalanceService.findCompanyBalanceList(queryInfo, userId);
		return resultList.toDatatables(null);
	}

}
